package com.che168.barchartdemo.utils.annotation;

import android.app.Activity;
import android.view.View;


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * BaseView注入工具
 *
 * @author zhudeshuai
 * @date 2015/12/1
 */
public class ViewUtil {

    /**
     * BaseView 注入方法（findViewById,setOnclickListener）
     *
     * @param baseView
     */
    public static void injectBaseView(final Activity baseView) {
        Class aClass = baseView.getClass();
        //解析属性上的注解
        Field[] fields = aClass.getDeclaredFields();
        if (fields != null && fields.length > 0) {
            for (Field field : fields) {
                if (field.isAnnotationPresent(FindView.class)) {
                    FindView annotation = field.getAnnotation(FindView.class);
                    int value = annotation.value();
                    final String methodName = annotation.click();
                    View viewById = baseView.findViewById(value);
                    if (viewById != null) {
                        field.setAccessible(true);
                        try {
                            field.set(baseView, viewById);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                    if (methodName != null && methodName.length() > 0) {
                        try {
                            Object object = field.get(baseView);
                            if (object instanceof View) {
                                ((View) object).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        invokeMethod(baseView, methodName, v);
                                    }
                                });
                            }
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }

                    }
                }
            }
        }


    }

    /**
     * 执行方法
     *
     * @param baseView   BaseView对象
     * @param methodName 执行的方法名
     * @param params     参数
     */
    private static void invokeMethod(Activity baseView, String methodName, Object... params) {
        if (baseView.getClass() == null)
            return;
        try {
            Method declaredMethod = baseView.getClass().getDeclaredMethod(methodName, View.class);
            declaredMethod.setAccessible(true);
            if (declaredMethod != null) {
                declaredMethod.invoke(baseView, params);
            } else {
                throw new RuntimeException("no such method:" + methodName);
            }

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
